import request from '@/utils/request'
import defaultSettings from '@/settings'
import store from '@/store'
const Sm4js = require('sm4js')

export function login(data) {
  if (defaultSettings.userModule) {
    // 密码加密
    var sm4Config = {
      key: store.getters.sm4Key,
      mode: 'ecb',
      cipherType: 'base64'
    }
    var sm4 = new Sm4js(sm4Config)
    var j_password = data['j_password']
    var password = sm4.encrypt(j_password)
    data['j_password'] = password

    return request({
      // url: '/vue-element-admin/user/login',
      url: '/app/authentication',
      method: 'post',
      params: data
    })
  } else {
    return request({
      url: '/vue-element-admin/user/login',
      method: 'post',
      params: data
    })
  }
}

export function getInfo(token) {
  return request({
    url: '/vue-element-admin/user/info',
    method: 'get',
    params: { token }
  })
}

export function logout() {
  if (defaultSettings.userModule) {
    return request({
      // url: '/vue-element-admin/user/logout',
      url: '/app/logout',
      method: 'get'
    })
  } else {
    return request({
      url: '/vue-element-admin/user/logout',
      method: 'post'
    })
  }
}
